<template>
  <div class="course-content-list">
    <van-pull-refresh
      v-model="isRefresh"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in list"
          :key="item.id"
          @click="$router.push({
            name: 'course-info',
            params: {
              courseId: item.id
            }
          })">
          <!-- 左侧图片 -->
          <div class="course-img">
            <!-- 所有课程与已购课程数据属性名不同,检测后使用 -->
            <img class="courseImgUrl" :src="item.courseImgUrl || item.image">
          </div>
          <!-- 右侧信息 -->
          <div class="course-right">
            <h3 class="course-courseName" v-text="item.courseName || item.name"></h3>
            <p class="course-preview" v-html="item.previewFirstField"></p>
            <p class="course-container" v-if="item.price">
              <span class="course-discounts">￥ {{ item.discounts }}</span>
              <s class="course-price">￥ {{ item.price }}</s>
            </p>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
// import { getQueryCourses } from '@/services/course'
export default {
  name: 'CourseContentList',
  props: {
    // 用于请求数据的函数
    fetchData: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      // 用于存储数据
      list: [],
      // 控制加载状态
      loading: false,
      // 是否加载完毕
      finished: false,
      // 数据页数
      currentPage: 1,
      // 下来刷新状态
      isRefresh: false
    }
  },
  // created () {
  // },
  methods: {
    async onRefresh () {
      // 还原数据页数为一
      this.currentPage = 1
      // 重新请求数据
      const { data } = await this.fetchData({
        currentPage: this.currentPage,
        pageSize: 10,
        status: 1
      })
      if (data.data && data.data.records && data.data.records.length !== 0) {
        this.list = data.data.records
      } else if (data.content && data.content.length !== 0) {
        this.list = data.content
      }
      this.$toast('刷新成功')
      this.isRefresh = false
    },
    async onLoad () {
      // 发送请求，请求上架课程信息
      const { data } = await this.fetchData({
        currentPage: this.currentPage,
        pageSize: 10,
        status: 1
      })
      // console.log(data)
      if (data.data && data.data.records && data.data.records.length !== 0) {
        this.list.push(...data.data.records)
      } else if (data.content && data.content.length !== 0) {
        this.list.push(...data.content)
      }
      // 下次请求下一页
      this.currentPage++
      // 加载状态结束
      this.loading = false

      // 数据全部加载完成
      if (data.data && data.data.records && data.data.records.length < 10) {
        this.finished = true
      } else if (data.content && data.content.length < 10) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.course-content-list {
  position: fixed;
  overflow-y: auto;
  right: 0;
  bottom: 50px;
  left: 0;
  // 课程列表项目的样式
  .course-img {
    img {
      width: 94px;
      height: 123px;
      float: left;
      border-radius: 4px;
    }
  }
  .course-right{
    padding-left: 20px;
    display: flex;
    flex-direction: column;
    flex: 1;
    height: 100%;
    .course-courseName {
      color: #333;
      font-size: 18px;
      line-height: 25px;
      margin: 0;
    }
    .course-preview {
      color: #999;
      font-size: 12px;
      // margin-top: 4px;
      line-height: 17px;
      flex-grow: 1;
      overflow: hidden;
      text-overflow:ellipsis;
      white-space: nowrap;
    }
    .course-container {
      // margin-top: 46px;
      margin-bottom: 0;
      height: 28px;
      line-height: 28px;
      .course-discounts {
        font-size: 20px;
        color: #ff7452;
        display: inline-block;
        margin-right: 5px;
      }
      .course-price {
        font-size: 12px;
        color: #999;
        margin-right: 10px;
      }
    }
  }
}
</style>
